using System;
using System.Web.Http;
using System.Web.Http.Cors;
using SmartLink.Infrastructure.Mappers;
using SmartLink.WebApi.App_Start;
using SmartLink.WebApi.Filter;

namespace SmartLink.WebApi
{
    public static class WebApiConfig
    {
        public static void Register(HttpConfiguration config)
        {
            //启用跨域
            var cors = new EnableCorsAttribute("*", "*", "*");
            config.EnableCors(cors);

            //GlobalConfiguration.Configure(configuration => configuration.BindParameter(typeof(DateTime), new UtcDateTimeModelBinder()));
            //Bootstrapper.CoreAutoFacInit();


            // Web API 配置和服务
            // 将 Web API 配置为仅使用不记名令牌身份验证。
            //config.SuppressDefaultHostAuthentication();

            #region 自定义的过滤器 yhb
            config.Filters.Add(new GlobalActionFilterAttribute());
            config.Filters.Add(new TokenFilterAttribute());
            config.Filters.Add(new ApiHandleErrorAttribute());
            config.Filters.Add(new FirstLoginCheckAttribute());
            #endregion

            GlobalConfiguration.Configuration.Formatters.JsonFormatter.SerializerSettings.Converters.Add(
            new Newtonsoft.Json.Converters.IsoDateTimeConverter()
            {
                DateTimeFormat = "yyyy-MM-dd HH:mm:ss",
            });

            GlobalConfiguration.Configuration.Formatters.JsonFormatter.SerializerSettings.Converters.Add(
            new Newtonsoft.Json.Converters.IsoDateTimeConverter()
            {
                DateTimeFormat = "yyyy-MM-dd HH:mm:ss",
            });


            // Web API 路由
            config.MapHttpAttributeRoutes();

            config.Routes.MapHttpRoute(
                name: "DefaultApi",
                routeTemplate: "api/{controller}/{action}/{id}",
                defaults: new { id = RouteParameter.Optional }
            );
        }
    }
}
